package com.ruanda.controller; /**
 * 轮播图业务控制层
 */

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruanda.domain.IndexImg;
import com.ruanda.model.Result;
import com.ruanda.service.IndexImgService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "轮播图接口管理")
@RequestMapping("admin/indexImg")
@RestController
public class IndexImgController {
    @Autowired
    private IndexImgService indexImgService;
    
    /**
     * 多条件分页查询轮播图
     * @param current 当前页
     * @param size 每页显示条数
     * @param status 状态
     * @return
     */
    @Operation(summary = "多条件分页查询轮播图")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('admin:indexImg:page')")
    public Result<Page<IndexImg>> LoadIndexImgPage(@RequestParam Long current,
                                                   @RequestParam Long size,
                                                   @RequestParam(required = false) Integer status) {
        // 创建轮播图分页对象
        Page<IndexImg> page = new Page<>(current, size);
        // 多条件分页查询轮播图
        page = indexImgService.page(page, new LambdaQueryWrapper<IndexImg>()
                .eq(ObjectUtil.isNotNull(status), IndexImg::getStatus, status)
                .orderByDesc(IndexImg::getSeq)
        );
        return Result.success(page);
    }

    @Operation(summary = "新增轮播图")
    @PostMapping
    @PreAuthorize("hasAuthority('admin:indexImg:save')")
    public Result<String> saveIndexImg(@RequestBody IndexImg indexImg) {
        Boolean saved = indexImgService.saveIndexImg(indexImg);
        return Result.handle(saved);
    }

    @Operation(summary = "根据标识查询轮播图信息")
    @GetMapping("info/{imgId}")
    @PreAuthorize("hasAuthority('admin:indexImg:info')")
    public Result<IndexImg> loadIndexImgInfo(@PathVariable Long imgId) {
        IndexImg indexImg = indexImgService.queryIndexImgInfoById(imgId);
        return Result.success(indexImg);
    }

    @Operation(summary = "修改轮播图内容")
    @PutMapping
    @PreAuthorize("hasAuthority('admin:indexImg:update')")
    public Result<String> modifyIndexImg(@RequestBody IndexImg indexImg) {
        Boolean modified = indexImgService.modifyIndexImg(indexImg);
        return Result.handle(modified);
    }

    @Operation(summary = "批量删除轮播图")
    @DeleteMapping
    @PreAuthorize("hasAuthority('admin:indexImg:delete')")
    public Result<String> removeIndexImg(@RequestBody List<Long> imgIds) {
        Boolean removed = indexImgService.removeIndexImgByIds(imgIds);
        return Result.handle(removed);
    }
    
    /////// 小程序 //////////
    @Operation(summary = "查询小程序轮播图列表")
    @GetMapping("indexImgs")
    public Result<List<IndexImg>> loadWxIndexImgList() {
        List<IndexImg> indexImgList = indexImgService.queryWxIndexImgList();
        return Result.success(indexImgList);
    }
    
}